﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_AddPCHINControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        CustomGridView CustomGridView1;
        Label Label1;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            IninControl();
            if (!base.Page.IsPostBack)
            {
                Bind();
            }
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            CustomGridView1.RowDeleting += new GridViewDeleteEventHandler(CustomGridView1_RowDeleting);
            foreach (GridViewRow gvr in CustomGridView1.Rows) {
                Button btn_tj = (Button)gvr.FindControl("btn_tj");
                Button btn_tj2 = (Button)gvr.FindControl("btn_tj2");
                btn_tj.Click += new EventHandler(ChangeStatus);
                btn_tj2.Click += new EventHandler(ChangeStatus2);
            }
        }
        protected void IninControl() {
            CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            Label1 = (Label)FindControl("Label1");
        }
        protected void Bind() {
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_RowCreated);
            CustomGridView1.RowDataBound += new GridViewRowEventHandler(CustomGridView1_RowDataBound);
            DataTable dt = _EluZSDataProvider.GetZSPCHXX(base.Page.Request["PNID"]);
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.DataKeyNames = new string[] { "pcid", "提交状态" };
            CustomGridView1.DataBind();
            string tem = _EluZSDataProvider.GetDHZKX(base.Page.Request["PNID"]);
            if (!string.IsNullOrEmpty(tem))
            {
                Label1.Text = "项目待拨付额度为：" + string.Format("{0:N}", tem) + "元。";
            }
        }

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void CustomGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strid = CustomGridView1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("zs_zspch", "pcid", strid);
            if (del)
            {
                Bind();
                Antu.Utilities.UtilitiesPage.AlertDialog("删除成功", "<script>window.location.href=window.location.href;</script>");
            }
        }

        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string temp = rowview["提交状态"].ToString();
                if (temp == "1")
                {
                    e.Row.Cells[8].Enabled = false;
                    e.Row.Cells[8].Text = "删除";
                    e.Row.Cells[5].Enabled = false;
                    e.Row.Cells[5].Text = "选择";
                }
            }
        }

        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            foreach (GridViewRow gvr in CustomGridView1.Rows)
            {
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["提交状态"].ToString() == "0")
                {
                    Button btn = (Button)gvr.FindControl("btn_tj");
                    btn.Enabled = true;
                }
                else
                {
                    Button btn2 = (Button)gvr.FindControl("btn_tj2");
                    btn2.Enabled = true;
                }
                string strpcid = CustomGridView1.DataKeys[gvr.RowIndex].Values["pcid"].ToString();
                string tt = _EluZSDataProvider.GetSomeStr("zs_change_money", "count(*)", "pcid", strpcid);
                if (!string.IsNullOrEmpty(tt))
                {
                    if (Convert.ToInt32(tt) > 0) {
                        Button btn2 = (Button)gvr.FindControl("btn_tj2");
                        btn2.Enabled = false;
                    }
                }
            }
        }

        protected void ChangeStatus(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pcID = CustomGridView1.DataKeys[gvr.RowIndex].Values["pcid"].ToString();
            bool change = _EluZSDataProvider.UpdateBSZT("zs_zspch", "提交状态", "1", "pcid", pcID);
            if (change)
            {
                Bind();
            }
        }
        protected void ChangeStatus2(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string pcID = CustomGridView1.DataKeys[gvr.RowIndex].Values["pcid"].ToString();
            bool change = _EluZSDataProvider.UpdateBSZT("zs_zspch", "提交状态", "0", "pcid", pcID);
            if (change) { Bind(); }
        }
    }
}


